package com.scitc.pleasurevideo.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.scitc.pleasurevideo.R;
import com.scitc.pleasurevideo.helper.ForwardHelper;
import com.scitc.pleasurevideo.imgutil.ImageManager;
import com.scitc.pleasurevideo.pojo.DoubanTvVo;

import java.util.List;

public class HomeHorGridviewAdapter extends ArrayAdapter<DoubanTvVo> {

    private List<DoubanTvVo> items;
    private Context context;

    public HomeHorGridviewAdapter(@NonNull Context context, List<DoubanTvVo> items) {
        super(context, 0);
        this.context = context;
        this.items = items;
    }

    @Override
    public int getCount() {
        if (items == null || items.size() < 1) {
            return 0;
        }
        return items.size();
    }

    @Nullable
    @Override
    public DoubanTvVo getItem(int position) {
        if (position < 0 || position > items.size()) {
            return null;
        }
        return items.get(position);
    }

    public void deleteItem(String code) {
        if (code == null || code.trim().length() < 1) {
            return;
        }

        for (int i = 0; i < items.size(); i++) {

            if (items.get(i).getId().equals(code.trim())) {

                items.remove(i);
                break;

            }

        }

        notifyDataSetChanged();
    }

    public void clearItem() {
        items.clear();
        notifyDataSetChanged();
    }

    public void addAll(List<DoubanTvVo> items) {
        if (items == null || items.size() < 1) {
            return;
        }
        this.items.clear();
        this.items.addAll(items);
        notifyDataSetChanged();
    }

    class ViewHodler {
        ImageView imageView;
        TextView titleText, directorsText, castsText;
    }

    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        final DoubanTvVo news = items.get(position);
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        convertView = layoutInflater.inflate(R.layout.item_history_tv_item_col, parent, false);
        ViewHodler holder = new ViewHodler();
        holder.imageView = convertView.findViewById(R.id.imageView);
        holder.titleText = convertView.findViewById(R.id.titleText);
        holder.directorsText = convertView.findViewById(R.id.directorsText);
        holder.castsText = convertView.findViewById(R.id.castsText);

        ImageManager.bindRoundedCornersImageCenterCrop(context, holder.imageView, news.getCover(), 4);
        holder.titleText.setText(news.getTitle());
        holder.directorsText.setVisibility(View.GONE);
        holder.castsText.setVisibility(View.GONE);


        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ForwardHelper.toTvMenuContentActivity((Activity) context, news);
            }
        });

        return convertView;
    }
}
